| Conditions | 17 |
| Paths | 60 |
| Total Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like page.js ➔ page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import path from 'path' |
||
| 10 | var page = function (req, res) { |
||
| 11 | var html = (req.query.html) ? true : false |
||
| 12 | var json = null |
||
| 13 | var editor = false |
||
| 14 | if(typeof req.body.json !== 'undefined' && req.body.json !== null) { |
||
| 15 | editor = true |
||
| 16 | if(typeof req.body.json === 'string') { |
||
| 17 | json = JSON.parse(req.body.json) |
||
| 18 | }else { |
||
| 19 | json = req.body.json |
||
| 20 | } |
||
| 21 | } |
||
| 22 | |||
| 23 | var filepath = req.originalUrl.replace('/abe/page', '') |
||
| 24 | |||
| 25 | if(typeof filepath !== 'undefined' && filepath !== null) { |
||
| 26 | var jsonPath = null |
||
| 27 | var linkPath = null |
||
| 28 | |||
| 29 | var filePathTest = cmsData.revision.getDocumentRevision(filepath) |
||
| 30 | if(typeof filePathTest !== 'undefined' && filePathTest !== null) { |
||
| 31 | jsonPath = filePathTest.path |
||
| 32 | linkPath = filePathTest.abe_meta.link |
||
| 33 | } |
||
| 34 | |||
| 35 | if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { |
||
| 36 | res.status(404).send('Not found') |
||
| 37 | return |
||
| 38 | } |
||
| 39 | |||
| 40 | if(typeof json === 'undefined' || json === null) { |
||
| 41 | json = cmsData.file.get(jsonPath) |
||
| 42 | } |
||
| 43 | |||
| 44 | let meta = config.meta.name |
||
| 45 | |||
| 46 | var templateId = '' |
||
| 47 | if(json[meta] && json[meta].template) { |
||
| 48 | templateId = json[meta].template |
||
| 49 | }else { |
||
| 50 | templateId = req.params[0] |
||
| 51 | } |
||
| 52 | var text = cmsTemplates.template.getTemplate(templateId, json) |
||
| 53 | |||
| 54 | if (!editor) { |
||
| 55 | |||
| 56 | cmsData.source.getDataList(path.dirname(linkPath), text, json) |
||
| 57 | .then(() => { |
||
| 58 | var page = new Page(templateId, text, json, html) |
||
| 59 | res.set('Content-Type', 'text/html') |
||
| 60 | res.send(page.html) |
||
| 61 | }).catch(function(e) { |
||
| 62 | console.error(e) |
||
| 63 | }) |
||
| 64 | }else { |
||
| 65 | text = cmsData.source.removeDataList(text) |
||
| 66 | var page = new Page(templateId, text, json, html) |
||
| 67 | res.set('Content-Type', 'text/html') |
||
| 68 | res.send(page.html) |
||
| 69 | } |
||
| 70 | }else { |
||
| 71 | // not 404 page if tag abe image upload into each block |
||
| 72 | if(/upload%20image/g.test(req.url)) { |
||
| 73 | var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' |
||
| 74 | var img = new Buffer(b64str, 'base64') |
||
|
|
|||
| 75 | |||
| 76 | res.writeHead(200, { |
||
| 77 | 'Content-Type': 'image/jpeg', |
||
| 78 | 'Content-Length': img.length |
||
| 79 | }) |
||
| 80 | res.end(img) |
||
| 81 | }else { |
||
| 82 | res.status(404).send('Not found') |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | export default page |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.